package com.tt.fashionWebSite.dao.jpa;

import com.tt.fashionWebSite.dao.ChiTietHoaDonDao;
import com.tt.fashionWebSite.dao.HoaDonFullDao;
import com.tt.fashionWebSite.domain.ChiTietHoaDonFull;
import com.tt.fashionWebSite.domain.HoaDonFull;
import com.tt.fashionWebSite.dto.ChiTietHoaDonFullDto;
import com.tt.fashionWebSite.dto.HoaDonFullDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by isoft-work 5 on 12/24/2014.
 */
@Repository("hoaDonFull")
public class HoaDonFullJpaDao extends GenericJpaDao<HoaDonFull, String> implements HoaDonFullDao {
   @Autowired
   @Qualifier("chiTietHoaDon")
   private ChiTietHoaDonDao chiTietHoaDonDao;

   public HoaDonFullJpaDao(){
      super(HoaDonFull.class);
   }

   @Override
   public HoaDonFull getDetailBill(String idHD, String userName) {
      return getEntityManager().createNamedQuery(HoaDonFull.GET_BILL_DETAIL, HoaDonFull.class)
            .setParameter("userName", userName)
            .setParameter("idHD", idHD)
            .getResultList().get(0);
   }

   @Override
   public List<HoaDonFullDto> getAllDetailBill() {
      String strQuery = "SELECT "
            + "s.MA_HD, "
            + "s.TRANG_THAI "
            + "FROM hoa_don s ";

      Query query = getEntityManager().createNativeQuery(strQuery);
      List<Object> results = query.getResultList();
      List<HoaDonFullDto> hoaDonFullDtos = new ArrayList<HoaDonFullDto>();
      HoaDonFullDto hoaDonFullDto = null;
      for (int i = 0; i < results.size(); i++) {
         Object[] userDB = (Object[]) results.get(i);
         hoaDonFullDto = new HoaDonFullDto(userDB);

         List<ChiTietHoaDonFullDto> chiTietHoaDonFullDtos = chiTietHoaDonDao.getBillDetailByIdBill(userDB[0].toString());
         hoaDonFullDto.setChiTietHoaDon(chiTietHoaDonFullDtos);
         hoaDonFullDtos.add(hoaDonFullDto);
      }
      return hoaDonFullDtos;
   }
}
